/*********************/
/** AuthenticateActivity.java */
/*********************/
package atag.apps.mainApp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import atag.apps.mainApp.client.QueryPortPortPort;
import atag.apps.mainApp.client.QueryPortService;
import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;

public class AuthenticateActivity extends Activity {

   private NodeInfo m_myState;
   private DataPool m_dataPool;
   
   QueryPortService service = new QueryPortService();
   QueryPortPortPort port = service.getQueryPortPort();

final Handler h_AuthenticateActivityHandler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
       // Add code to handle messages sent from another thread to change the Activity UI
       // You may use this handler to send data recieved in the corresponding task
   }
};

/** Handles event: Button loginB clicked. */
private final OnClickListener loginBClicked = new OnClickListener() {

	public void onClick(View v) {

		Intent intent;
		try {

			EditText usernameET = ((EditText) findViewById(R.id.usernameET));
			EditText passwordET = ((EditText) findViewById(R.id.passwordET));
			EditText partitionET = ((EditText) findViewById(R.id.partitionET));

			String username = usernameET.getText().toString();
			String password = passwordET.getText().toString();
			String partition = partitionET.getText().toString();
			String ID = port.authenticateQuery(username, password, partition);
			if (ID.equals("-1")) {
				intent = new Intent(AuthenticateActivity.this, LoginFail.class);
			} else {
				intent = new Intent(AuthenticateActivity.this, ControlLightSettingActivity.class);
				intent.putExtra("username", username);
				intent.putExtra("partition", partition);
				intent.putExtra("ID", ID);
			}
			startActivity(intent);
		} catch (Exception e) {
			Context context = getApplicationContext();
			CharSequence text = "Exception:" + e;
			int duration = Toast.LENGTH_LONG;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			Log.e("Exception", e + "", e);
		}
	}
};

   /*
   * Creates options menu.
   *
   * @param menu the Menu to be populated
   */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.options_menu, menu);
       return true;
   }

   /*
   * Handles menu item selections
   *
   * @param item the selected MenuItem
   */
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
           case R.id.settings:
               return true;
           case R.id.exit:
               moveTaskToBack(true);
               return true;
       }
       return false;
   }


   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //Please add a layout in the res directory and also
       // add this activity in the manifest
       setContentView(R.layout.authenticate);
       SharedAndroidActivityHandlers.h_AuthenticateActivityHandler=h_AuthenticateActivityHandler; 
       this.m_dataPool = SharedStoreForGUITasks.m_dataPool;
       this.m_myState = SharedStoreForGUITasks.m_myState;
        Button loginB = (Button) findViewById(R.id.loginB);
		loginB.setOnClickListener(loginBClicked);
       }
 }
